package com.daoImpl;

import com.bean.Article;
import com.dao.ArticleDAO;
import com.dao.BasicDAO;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author 徐洲
 * @version 1.0
 */
public class ArticleDAOImpl extends BasicDAO<Article> implements ArticleDAO {
    @Test
    public void test() {
        String sql = "SELECT * FROM articlelist WHERE article_id = ?;";
        Article o = (Article) querySingle(sql, Article.class, "1");
        System.out.println(o.getArticle_title());
    }

    /**
     * 搜索
     */
    @Override
    public List<Article> getArticleByName(String keyword) {
        String sql = "{call getByNameProc(?)}";
        if (keyword == null) {
            keyword = "";
        }
        return queryMulti(sql, Article.class, keyword);
    }

    /**
     * 得到文章详细信息
     */
    @Override
    public Article getArticleById(String article_id) {
        String sql = "SELECT * FROM articlelist WHERE article_id = ?;";
        return (Article) querySingle(sql, Article.class, article_id);
    }

    /**
     * 得到我的文章
     */
    @Override
    public List<Article> getArticleByMy(String username) {
        String sql = "SELECT * FROM articlelist WHERE username = ?;";
        return queryMulti(sql, Article.class, username);
    }

    /**
     * 得到我的收藏文章
     */
    @Override
    public List<Article> getArticleByCollection(Object userid) {
        String sql = "SELECT DISTINCT articlelist.* FROM articlelist, article WHERE articlelist.article_title = article.article_title AND  article.article_id in (SELECT article_id FROM collect WHERE collect.user_id = ?)";
        return queryMulti(sql, Article.class, userid);
    }

    /**
     * 收藏文章
     */
    @Override
    public boolean collectArticle(Object userid, String article_id) {
        String sql = "INSERT INTO collect VALUES (null, ?, ?);";
        Integer integer = update(sql, userid, article_id);
        if (integer == null) return false;
        return integer > 0;
    }

    /**
     * 取消收藏文章
     */
    @Override
    public boolean noCollectArticle(Object userid, String article_id) {
        String sql = "DELETE FROM collect WHERE user_id = ? AND article_id = ?;";
        Integer integer = update(sql, userid, article_id);
        if (integer == null) return false;
        return integer > 0;
    }

//    文章

    /**
     * 发表文章
     */
    @Override
    public boolean addArticle(String article_title, Object user_id, Object category_id, String article_cover, String content) {
        String sql = "insert into article (article_title, user_id, category_id, article_cover, content) VALUES (?, ?, ?, ?, ?);";
        if ("".equals(article_cover) || article_cover==null) article_cover = "img.png";
        Integer integer = update(sql, article_title, user_id, category_id, article_cover, content);
        if (integer == null) return false;
        return integer > 0;
    }

    /**
     * 修改文章
     */
    @Override
    public boolean editArticle(Object article_id, String article_title, Object user_id, Object category_id, String article_cover, String content) {
        Integer integer;
        if ("".equals(article_cover) || (article_cover == null)) {
            String sql = "update article SET article_title = ?, user_id = ?, category_id = ?, content =? WHERE article_id = ?;";
            integer = update(sql, article_title, user_id, category_id, content, article_id);
        } else {
            String sql = "update article SET article_title = ?, user_id = ?, category_id = ?, article_cover =?, content =? WHERE article_id = ?;";
            integer = update(sql, article_title, user_id, category_id, article_cover, content, article_id);
        }
        if (integer == null) return false;
        return integer > 0;
    }

    /**
     * 删除文章
     */
    @Override
    public boolean deleteAllArticle(String[] article_ids) {
        StringBuilder condition = new StringBuilder();
        for (int i = 0; i < article_ids.length; i++) {
            condition.append(article_ids[i]);
            if (i != article_ids.length - 1) condition.append(",");
        }
        String sql = "delete from article where article_id in(" + condition + ")";
        Integer integer = update(sql);
        if (integer == null) return false;
        return integer > 0;
    }
}
